package org.example.dao.dto.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.example.dao.dao.user.*;
import org.example.dao.entity.user.*;
import org.example.skytool.commom.IdWorker;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 22449
 */
@Component
public class UserFactory {

    @Resource
    private IdWorker idWorker;

    @Resource
    private AuRoleDao uRoleDao;

    @Resource
    private AuPermissionDao uPermissionDao;

    @Resource
    private AuUserRoleDao uUserRoleDao;

    @Resource
    private AuRolePermissionDao uRolePermissionDao;

    @Resource
    private ULabelDao uLabelDao;

    @Resource
    private UUserLabelDao uUserLabelDao;

    public UserDto createPermission(UserDto user) {
        user.setPermissionList(new ArrayList<>());
        user.setRoleList(new ArrayList<>());
        Long userId = user.getUser().getId();

        //查询用户角色
        LambdaQueryWrapper<AuUserRole> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AuUserRole::getUserId, userId);
        List<AuUserRole> listUserRole = uUserRoleDao.selectList(lambdaQueryWrapper);

        //如果没有权限，注入默认用户角色
        if (listUserRole.size() == 0) {
            listUserRole.add(createDefaultUserRole(userId));
        }



        //获取用户角色id列表
        List<Long> listRoleId = new ArrayList<>();
        for (AuUserRole uUserRole : listUserRole) {
            listRoleId.add(uUserRole.getRoleId());
        }


        //查询角色列表
        LambdaQueryWrapper<AuRole> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.in(AuRole::getId, listRoleId);
        List<AuRole> listRole = uRoleDao.selectList(lambdaQueryWrapper1);

        //查询角色权限列表
        LambdaQueryWrapper<AuRolePermission> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper2.in(AuRolePermission::getRoleId, listRoleId);
        List<AuRolePermission> listRolePermission = uRolePermissionDao.selectList(lambdaQueryWrapper2);

        //获取权限id列表
        List<Long> listPermissionId = new ArrayList<>();
        for (AuRolePermission uRolePermission : listRolePermission) {
            listPermissionId.add(uRolePermission.getPermissionId());
        }

        //获取权限列表
        if (listPermissionId.size() != 0) {
            LambdaQueryWrapper<AuPermission> lambdaQueryWrapper3 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper3.in(AuPermission::getId, listPermissionId);
            List<AuPermission> listPermission = uPermissionDao.selectList(lambdaQueryWrapper3);
            //注入权限到userDto
            for (AuPermission permission : listPermission) {
//                user.getPermissionList().add(permission.getName());
            }
        }

        //注入角色到userDto
        for (AuRole role : listRole) {
//            user.getRoleList().add(role.getName());
        }
        return user;
    }

    public UserDto createLabel(UserDto userDto){
        Long userId = userDto.getUser().getId();
        LambdaQueryWrapper<UUserLabel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UUserLabel::getUserId,userId);
        lambdaQueryWrapper.eq(UUserLabel::getIsValid,1);
        List<UUserLabel> labels = uUserLabelDao.selectList(lambdaQueryWrapper);

        if(labels==null||labels.size()==0){
            labels.add(createDefaultUserLabel(userId));
        }

        List<Long> listLabel = new ArrayList<>();
        for(UUserLabel temp:labels){
            listLabel.add(temp.getLabelId());
        }

        LambdaQueryWrapper<ULabel> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.in(ULabel::getId,listLabel);
        lambdaQueryWrapper1.eq(ULabel::getIsValid,1);

        List<ULabel> uLabels = uLabelDao.selectList(lambdaQueryWrapper1);
        userDto.setLabels(uLabels);
        return userDto;
    }



    public UserDto createUserDto(UUser user){
//        UserDto userDto = new UserDto(user, null, null);
//        userDto = createPermission(userDto);
//        return userDto;
    return null;
    }

    public UserDto createUserDto(UUser user, UStudent uStudent, String cookie) {
//        UserDto userDto = new UserDto(user, uStudent, cookie);
//        构建权限信息
//        return userDto;
    return null;
    }

    public UUserLabel createDefaultUserLabel(Long userId){
        UUserLabel uUserLabel = new UUserLabel(idWorker.nextId(),userId,1L);
        uUserLabelDao.insert(uUserLabel);
        return uUserLabel;
    }

    public AuUserRole createDefaultUserRole(Long userId) {
        AuUserRole uUserRole = new AuUserRole();
        uUserRole.setId(idWorker.nextId());
        uUserRole.setUserId(userId);
        uUserRole.setRoleId(1L);
        uUserRole.setIsValid(1);
        uUserRole.setCreateTime(new Date());
        uUserRole.setUpdateTime(new Date());
        uUserRoleDao.insert(uUserRole);
        return uUserRole;

    }


}
